<?php
    
	include_once "include/constants.php";
	
    define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
	if (!isset($_GET["mid"]) || !is_numeric($_GET["mid"])) die("Error: invalid input");
    
    echo "<script>movieId = " . $_GET["mid"] . "</script>";
    echo "<script>ownerId = " . $session->userinfo["id"] . "</script>";
    
    $thumbs_UserId = $session->userinfo["id"];
    include_once "mn_db_thumbs_get.php";    
    
    try {
        $conn = new PDO('mysql:host='.DB_SERVER.';dbname='.DB_NAME, DB_USER, DB_PASS);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    
         
        $stmt = $conn->prepare('select m.movieid, m.title, m.year, m.imdbid, t.taglinetext, group_concat(g.genre separator ", ") genres
                                from movies m
                                left outer join taglines t on (m.movieid = t.movieid)
                                left outer join genres g on (m.movieid = g.movieid)
                                where m.movieid = :movieid group by m.movieid;');
		$stmt->execute(array("movieid" => $_GET["mid"]));
		$movie = $stmt->fetch();
		$pattern = '/(?P<title>.+)\(\d{4}\/?\w*\)/';
		preg_match($pattern, $movie['title'], $matches);
        $movie['title'] = $matches['title'];
		if (isset($movie["genres"])) {
			$genres = explode(", ", $movie["genres"]);
		}
		else {
			$genres = array();
		}
  
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
	
	
	try {
        $conn = new PDO('mysql:host='.DB_SERVER.';dbname='.DB_NAME, DB_USER, DB_PASS);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    
         
        $stmt = $conn->prepare('select distinct "d" as type, d.directorid as id, d.name from movies2directors m2d, directors d
								where m2d.directorid = d.directorid
								and m2d.movieid = :movieid
								union
								select distinct "w" as type, w.writerid as id, w.name from movies2writers m2w, writers w
								where m2w.writerid = w.writerid
								and m2w.movieid = :movieid;');
		$stmt->execute(array("movieid" => $_GET["mid"]));
		
		$directors = array();
		$writers = array();
		while ($dw_data = $stmt->fetch())
		{
			if ($dw_data["type"] == "d")
			{
				$directors[] = array("id" => $dw_data["id"], "name" => parse_name($dw_data["name"]));
			}
			else if ($dw_data["type"] == "w")
			{
				$writers[] = array("id" => $dw_data["id"], "name" => parse_name($dw_data["name"]));
			}
		}
		
			
        
  
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
    
    try {
        $conn = new PDO('mysql:host='.DB_SERVER.';dbname='.DB_NAME, DB_USER, DB_PASS);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    
         
        $stmt = $conn->prepare('select * from user_ratings where userid = :userid and movieid = :movieid');
        
		$stmt->execute(array("userid" => $session->userinfo['id'], "movieid" => $_GET["mid"]));
		$rating = $stmt->fetch();
        
        $rating_int = 0;
        if (isset($rating["rating"])) {
            $rating_int = $rating["rating"];
        }
        
        echo "<script>movieRating = $rating_int;</script>";
  
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
	
	try {
        $conn = new PDO('mysql:host='.DB_SERVER.';dbname='.DB_NAME, DB_USER, DB_PASS);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    
         
        $stmt = $conn->prepare('select m2a.as_character, a.name, a.actorid from movies2actors m2a
								inner join actors a on (m2a.actorid = a.actorid) where m2a.movieid = :movieid;');
		$stmt->execute(array("movieid" => $_GET["mid"]));
		
		$actors = array();
		while ($a_data = $stmt->fetch())
		{
			$actors[] = array("id" => $a_data["actorid"], "name" => parse_name($a_data["name"]), "character" => parse_char($a_data["as_character"]));
		}
		uasort($actors, 'character_comp');
		
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
    
    
?>

<div class="container margin-top-bottom">
	<div class="row">
		<div class="span3">
		    <?php
                define('CURR_PAGE', 'moviedata');
                include 'mn_sidebar.php';
            ?>
		</div>
		<div class="span9">
		
			<div class="row chosen-movie">
                <div class="span3" style="text-align: center;">
                    <img src="img/posters/<?php echo $movie['imdbid']; ?>_large.jpg" alt="Poster" style='margin-bottom: 10px' class="shadow chosen-image">
                    <div class="rating" style="margin-bottom: 5px">
                        <i class="icon-star-empty rating-star" id="rating-star-1"></i>
                        <i class="icon-star-empty rating-star" id="rating-star-2"></i>
                        <i class="icon-star-empty rating-star" id="rating-star-3"></i>
                        <i class="icon-star-empty rating-star" id="rating-star-4"></i>
                        <i class="icon-star-empty rating-star" id="rating-star-5"></i>
                        <i class="icon-star-empty rating-star" id="rating-star-6"></i>
                        <i class="icon-star-empty rating-star" id="rating-star-7"></i>
                        <i class="icon-star-empty rating-star" id="rating-star-8"></i>
                        <i class="icon-star-empty rating-star" id="rating-star-9"></i>
                        <i class="icon-star-empty rating-star" id="rating-star-10"></i>
                    </div>
					<div style="margin: 0 auto; width: 180px;" class="btn-group">
						<button class="btn span1 movie-toolbar" data-desc="Seen it" data-toggle="button"><i class="icon-eye-open"></i></button>
						<button class="btn span1 movie-toolbar" data-desc="Liked it" data-toggle="button"><i class="icon-thumbs-up"></i></button>
						<button class="btn span1 movie-toolbar" data-desc="Recomended" data-toggle="button"><i class="icon-ok"></i></button>
					</div>
				</div>
                <div class="span5">
                    <h1 class="chosen-title">
						<?php echo $movie["title"]; ?>
                        <small><?php echo $movie["year"]; ?></small>
                    </h1>
                    
                    <h4 style="margin-bottom: 4px;" class="chosen-genre">
						<?php
							foreach ($genres as $index => $genre)
							{ 
								echo '<span class="rankable" data-rankable-type="Genre" data-rankable-value="' . array_search($genre, $genreId) . '">'.$genre
									.'</span>'.(($index < count($genres) - 1) ? ", " : "");
							} 
						
						?>
					</h4>
                    <p class="chosen-tagline"><?php if (isset($movie["taglinetext"])) { echo $movie["taglinetext"]; }?></p>
					<h4 style="margin-bottom: 4px;">
						<strong>Director<?php echo (count($directors) > 1) ? "s" : ""; ?>:</strong>
						<?php 
							foreach ($directors as $index => $dir)
							{ 
								echo '<span class="rankable" data-rankable-type="Director" data-rankable-value="'. $dir["id"] . '">'.$dir["name"]
									.'</span>'.(($index < count($directors) - 1) ? ", " : "");
							} 
						?>
					</h4>
					<h4 style="margin-bottom: 20px;">
						<strong>Writer<?php echo (count($writers) > 1) ? "s" : ""; ?>:</strong>
						<?php 
							foreach ($writers as $index => $writer)
							{ 
								echo '<span class="rankable" data-rankable-type="Writer" data-rankable-value="'. $writer["id"] . '">'.$writer["name"]
									.'</span>'.(($index < count($writers) - 1) ? ", " : "");
							} 
						?>
					</h4>
					<h3>Actors <small>(<a class="show-all" href="#">full cast</a>)</small></h3>
					<table class="table table-striped">
						<?php
							foreach ($actors as $actor) {
								$actorname = $actor["name"];
								$charname = $actor["character"]["char"];
								$r_value = $actor["id"];
								echo "<tr class=\"actor-row\"><td><span class=\"rankable\" data-rankable-type=\"Actor\" data-rankable-value=\"$r_value\">$actorname</span></td><td>$charname</td></tr>";
							}
						?>
					</table>
                </div>
            </div>
		
		</div>
	</div>
</div>
		